package com.risenb.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.risenb.base.BaseControllor;
import com.risenb.bean.Manager;
import com.risenb.constant.Const;

public class CustomerBaseControllor extends BaseControllor{
	/**
	 * 验证用户权限
	 */
	@Override
	public void validate(){
		HttpServletRequest request = request();
		HttpServletResponse response = response();
		HttpSession session = request.getSession();
		String path = request.getServletPath();
		String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
		if(path.matches(Const.NO_INTERCEPTOR_PATH_MANAGE)){//不需要用户登录要放过的资源
			
		}else{
			Manager manager = (Manager)session.getAttribute(Const.SESSION_MANAGER);
			if(null == manager ){//用户未登录 跳转到登录页面
				try {
					response.sendRedirect(request.getContextPath()+"/manage/login.im");
				} catch (IOException e) {
					e.printStackTrace();
				}
				return ;
			}
			Map<String,String> permissionMap = (Map<String, String>) session.getAttribute("permissionMap");
			String string = permissionMap.get(path);
			if(null == string || "".equals(string)){
				try {
					response.sendRedirect(request.getContextPath()+"/error/authority.im");
				} catch (IOException e) {
					e.printStackTrace();
				}
				return ;
			}
			
		}
	}
}
